package com.ruoyi.youtian.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.youtian.domain.Attribute;
import com.ruoyi.youtian.service.IAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 属性Controller
 *
 * @author ruoyi
 * @date 2022-09-06
 */
@RestController
@RequestMapping("/youtian/attribute")
public class AttributeController extends BaseController
{
    @Autowired
    private IAttributeService attributeService;

    /**
     * 查询属性列表
     */
//    @PreAuthorize("@ss.hasPermi('system:attribute:list')")
    @GetMapping("/list")
    public TableDataInfo list(Attribute attribute)
    {
        startPage();
        List<Attribute> list = attributeService.selectAttributeList(attribute);
        return getDataTable(list);
    }

    /**
     * 导出属性列表
     */
//    @PreAuthorize("@ss.hasPermi('system:attribute:export')")
    @Log(title = "属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attribute attribute)
    {
        List<Attribute> list = attributeService.selectAttributeList(attribute);
        ExcelUtil<Attribute> util = new ExcelUtil<Attribute>(Attribute.class);
        util.exportExcel(response, list, "属性数据");
    }

    /**
     * 获取属性详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:attribute:query')")
    @GetMapping(value = "/{attributeId}")
    public AjaxResult getInfo(@PathVariable("attributeId") Long attributeId)
    {
        return AjaxResult.success(attributeService.selectAttributeByAttributeId(attributeId));
    }

    /**
     * 新增属性
     */
//    @PreAuthorize("@ss.hasPermi('system:attribute:add')")
    @Log(title = "属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Attribute attribute)
    {
        return toAjax(attributeService.insertAttribute(attribute));
    }

    /**
     * 修改属性
     */
//    @PreAuthorize("@ss.hasPermi('system:attribute:edit')")
    @Log(title = "属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attribute attribute)
    {
        return toAjax(attributeService.updateAttribute(attribute));
    }

    /**
     * 删除属性
     */
//    @PreAuthorize("@ss.hasPermi('system:attribute:remove')")
    @Log(title = "属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attributeIds}")
    public AjaxResult remove(@PathVariable Long[] attributeIds)
    {
        return toAjax(attributeService.deleteAttributeByAttributeIds(attributeIds));
    }
}
